package com.kzd.gtgame.main.popup

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.entity.GameFeedback
import com.kzd.gtgame.main.game.adapter.GameFeedbackAdapter
import razerdp.basepopup.BasePopupWindow


class GameFeedbackPopup(
    context: Context,
    val onGameList: (view: View, gameFeedback: GameFeedback) -> Unit
) : BasePopupWindow(context) {

    init {
        popupGravity = Gravity.BOTTOM
        setOverlayNavigationBar(false)
    }

    private val mGameFeedbackAdapter = GameFeedbackAdapter()

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_game_bargain)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.findViewById<ImageView>(R.id.ivGameBargainClose).setOnClickAntiShakeListener {
            dismiss()
        }
    }

    override fun onCreateShowAnimation(width: Int, height: Int): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f,
            Animation.RELATIVE_TO_PARENT, 0f
        ).apply { duration = 300L }
    }

    override fun onCreateDismissAnimation(): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f
        ).apply { duration = 300L }
    }

    fun fillDataGameFeedback(gameFeedback: MutableList<GameFeedback>): GameFeedbackPopup {
        contentView.findViewById<TextView>(R.id.tvGameBargainTitle)
            .text = "选择游戏"
        contentView.findViewById<RecyclerView>(R.id.rvGameBargainBody)
            .adapter = mGameFeedbackAdapter.apply {
            setNewInstance(gameFeedback)
        }
        mGameFeedbackAdapter.setOnItemClickListener { _, view, position ->
            val itemData = mGameFeedbackAdapter.getItem(position)
            onGameList(view, itemData)
        }
        return this
    }

}